package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
 /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataKey extends NullField implements ITag {
	public static String DEFAULT_ID = "_Key";

	private GrfxMLEngine engine;

	public DataSize lastKeyPressed = new DataSize();
	public DataSize currentKeyPressed = new DataSize();
	public DataString keyBuffer = new DataString();

	GeometryText _focus=null;

	public static boolean ctrl = false;
	public static boolean alt = false;
	public static boolean shift = false;
	public static boolean[] kbd = new boolean[256];
	public static boolean[] fn = new boolean[12];
public DataKey(GrfxMLEngine engine) {

	this.engine = engine;

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
/**
 *
 * @return boolean
 */
public static boolean altDown() {
	return alt;
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.LAST_KEY_PRESSED))
		{
			lastKeyPressed = new DataSize();
			lastKeyPressed.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.CURRENT_KEY_PRESSED))
		{
			currentKeyPressed = new DataSize();
	  		currentKeyPressed.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.KEY_BUFFER))
		{
			keyBuffer = new DataString();
	  		keyBuffer.buildFromgrfxML(element);
		}
		else if( !NameHelper.isSystemName(element.getName()) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.KEY,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.LAST_KEY_PRESSED))
		{
			lastKeyPressed.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.CURRENT_KEY_PRESSED))
		{
	  		currentKeyPressed.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.KEY_BUFFER))
		{
	  		keyBuffer.copyFromgrfxML(element);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.KEY,""+element.getStartLineNo()} );
		}
	}
}
/**
 *
 * @return boolean
 */
public static boolean ctrlDown() {
	return ctrl;
}
/**
 *
 * @return boolean
 */
public static boolean fnKeyDown(char key) {
	if(key>12 || key<1)
		return false;
	return fn[(int)key-1];
}
public DataKey getClone() {

	DataKey newKey = new DataKey(engine);

	newKey.currentKeyPressed = currentKeyPressed.getClone();
	newKey.lastKeyPressed = lastKeyPressed.getClone();
	newKey.keyBuffer = keyBuffer.getClone();

	return newKey;
}
public DataKey getClone(String path) {

	DataKey newKey = new DataKey(engine);

	GrfxMLStoreProcessor.setCloneName( path,this,newKey );

	newKey.currentKeyPressed = currentKeyPressed.getClone(path);
	newKey.lastKeyPressed = lastKeyPressed.getClone(path);
	newKey.keyBuffer = keyBuffer.getClone(path);

	return newKey;
}
public DataSize getCurrentKeyPressed() {
	return currentKeyPressed;
}
public DataString getKeyBuffer() {
	return keyBuffer;
}
public DataSize getLastKeyPressed() {
	return lastKeyPressed;
}
/**
 *
 * @return boolean
 */
public static boolean keyDown(char key) {
	return kbd[(int)key];
}
public void register(DataLinker store) {

	try{
		store.add(lastKeyPressed,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(currentKeyPressed,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(keyBuffer,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	if(lastKeyPressed==oldObj)
		lastKeyPressed=(DataSize)newObj;

	if(currentKeyPressed==oldObj)
		currentKeyPressed=(DataSize)newObj;

	if(keyBuffer==oldObj)
		keyBuffer=(DataString)newObj;
}
public void resetChanged() {
	super.resetChanged();
	lastKeyPressed.resetChanged();
	currentKeyPressed.resetChanged();

	alt = false;
	ctrl = false;
	shift = false;

	try{
		if(
				engine!=null &&
				engine.BASE!=null &&
				engine.BASE.KEY!=null &&
				engine.BASE.KEY.keyBuffer!=null
			)
		{
			engine.BASE.KEY.keyBuffer.setValue("",this);
		}
	}
	catch(LockException le){}//dummy catch

	for(int loop=1;loop<kbd.length;loop++)
	{
		kbd[loop] = false;
	}

	for(int loop=0;loop<fn.length;loop++)
	{
		fn[loop] = false;
	}
}
protected void setCurrentKeyPressed(DataSize newCurrentKeyPressed) {
	currentKeyPressed = newCurrentKeyPressed;
}
protected void setKeyBuffer(DataString newKeyBuffer) {
	keyBuffer = newKeyBuffer;
}
protected void setLastKeyPressed(DataSize newLastKeyPressed) {
	lastKeyPressed = newLastKeyPressed;
}
/**
 *
 * @return boolean
 */
public static boolean shiftDown() {
	return shift;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += lastKeyPressed.togrfxML(Token.LAST_KEY_PRESSED);
	xml += currentKeyPressed.togrfxML(Token.CURRENT_KEY_PRESSED);
	xml += keyBuffer.togrfxML(Token.KEY_BUFFER);
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
